package aleksandar.djuric.online;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ProtocolException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import aleksandar.djuric.common.TimeManagement;
import aleksandar.djuric.entities.Attribute;
import aleksandar.djuric.entities.Player;
import aleksandar.djuric.entities.Team;

public class FIDataFetcher {
	
	private static final String fidUrl =
		"http://footballidentity.com";
	private static final String myLeaguePage =
		"/WorldAndCompetition" +
		"/StatisticsAndAwards/LeagueSpace.aspx";
	private static final String teamPage =
		"/Team/TeamGeneral/General/U71.aspx?id=";
	private static final String teamPlayersPage =
		"/Team/TeamGeneral/General/TeamSummaryPlayers.aspx?id=";
	private static final String myTeamPlayersPage =
		"/Team/TeamGeneral/TeamSpacePlayers.aspx";
	private static final String playerPage =
		"/Player/PlayerGeneral/General/U59.aspx?id=";
	private static final String matchStatsPage =
		"/WorldAndCompetition/StatisticsAndAwards/U84Players.aspx?id=";
//	private static final String myPlayerPage =
//		"/Player/PlayerGeneral/U58.aspx";
			
	
	private DefaultHttpClient client;
	private BasicResponseHandler responseHandler;
	private BufferedReader in;
	private String page;
	private String tmp;
	private HttpGet get;
	private String username;
	private String password;
	private int myId;
	private String myLeague;
	private String viewState;
    private String eventValidation;
    private String redirectUrl;

	// Constructor.
	public FIDataFetcher(String username, String password)
	{
		ThreadSafeClientConnManager connectionManager = 
			new ThreadSafeClientConnManager();
		connectionManager.setDefaultMaxPerRoute(20);
		client = new DefaultHttpClient(connectionManager);
		client.setRedirectStrategy(new DefaultRedirectStrategy()
		{
			@Override
			public HttpUriRequest getRedirect(HttpRequest request,
					HttpResponse response, HttpContext context)
					throws ProtocolException {
				HttpUriRequest newRequest = super.getRedirect(request, response, context);
				redirectUrl = newRequest.getURI().toString();
				return newRequest;
			}
		});
		responseHandler = new BasicResponseHandler();
		this.username = username;
		this.password = password;
	}
	
	
	
	// Login to server.
	public int login(){
		int returnValue = -1;
		try {
			// Get required parameters for post request.
			get = new HttpGet(fidUrl +
					"/BasicWebsite/LogOnAndPresentation/U1.aspx");
	
	        page = client.execute(get, responseHandler);
	        in = new BufferedReader(new StringReader(page));
	        tmp = "";
	        while((tmp = in.readLine()) != null)
	        {
	        	if(tmp.contains("__VIEWSTATE"))
	        	{
//	        		System.out.println(tmp);
	        		viewState = tmp.substring(
	        				tmp.lastIndexOf("value=\"") + 7,
	        				tmp.lastIndexOf("\""));
//	        		System.out.println(viewState);
	        	}
	        	else if(tmp.contains("__EVENTVALIDATION"))
	        	{
//	        		System.out.println(tmp);
	        		eventValidation = tmp.substring(
	        				tmp.lastIndexOf("value=\"") + 7,
	        				tmp.lastIndexOf("\""));
//	        		System.out.println(eventValidation);
	        	}
	        }
			
	        // Login to server.

			HttpPost httpost = new HttpPost(fidUrl +
					"/BasicWebsite/LogOnAndPresentation/U1.aspx");

	
	        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
//	        nvps.add(new BasicNameValuePair(
//	        		"__LASTFOCUS", ""));
//	        nvps.add(new BasicNameValuePair(
//	        		"__EVENTTARGET", ""));
//	        nvps.add(new BasicNameValuePair(
//	        		"__EVENTARGUMENT", ""));
	        
	        nvps.add(new BasicNameValuePair("__VIEWSTATE", viewState));
	        nvps.add(new BasicNameValuePair(
	        		"__EVENTVALIDATION", eventValidation));
//	        nvps.add(new BasicNameValuePair(
//	        		"langList", "2057"));
//	        nvps.add(new BasicNameValuePair(
//	        		"ClientTimeZoneOffsetValue", "-120"));
	        
	        nvps.add(new BasicNameValuePair("logOnMain$UserName", username));
	        nvps.add(new BasicNameValuePair("logOnMain$Password", password));
	        
	        nvps.add(new BasicNameValuePair("logOnMain$LoginButton", ""));
	        
//	        nvps.add(new BasicNameValuePair(
//	        		"DXScript", "1_20,1_21,1_18"));
//	        nvps.add(new BasicNameValuePair(
//	        		"DXCss", "/Styles/styles.css?version=1"));

        
			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
			HttpResponse response = client.execute(httpost);
			if(response.getStatusLine().toString().equals(
					"HTTP/1.1 302 Found"))
				returnValue = 0;
			else if(response.getStatusLine().toString().equals(
					"HTTP/1.1 200 OK"))
				returnValue = 1;
			else
				returnValue = 2;
			EntityUtils.consume(response.getEntity());
			
		} catch (Exception e)
		{
			e.printStackTrace();
			returnValue = -1;
		}
		return returnValue;
	}
	
	// Fetch player data from player page.
	public boolean getPlayer(Player player){
		try
		{
			HttpGet get = new HttpGet(fidUrl + playerPage + player.getId());
			
			redirectUrl = "";
	        String page = client.execute(get, responseHandler);
	        
	        if(redirectUrl.contains("BasicWebsite/LogOnAndPresentation/U1.aspx"))
	        {
	        	return false;
	        }
	        
	        BufferedReader in = new BufferedReader(
	        		new StringReader(page));
	        String tmp = "";
	        Attribute[] attribs = new Attribute[Player.N_ATTRIBS];
	        int attribNo = 0;
	        String userId = null;
	        player.setMyChar(false);
	        while((tmp = in.readLine()) != null)
	        {
	        	if(tmp.contains("M_M_M_C_C_C_labelBankAccountBalance"))
	        	{
	        		player.setMyChar(true);
	        	}
	        	else if(tmp.contains("class=\"dxgvIndentCell dxgv\"") ||
	        			tmp.contains("<td class=\"cellLeft\">"))
	        	{
	        		for (attribNo = 0; attribNo < attribs.length; attribNo++) {
	        			if(tmp.contains(
	        					">" + Player.attribNames[attribNo] +"<"))
	        			{
	        				tmp = extractValue(tmp);
	        				try
	        				{
	        					double value = Double.parseDouble(tmp);
	        					attribs[attribNo] = new Attribute(value);
	        				}
	        				catch(NumberFormatException e)
	        				{
	        					attribs[attribNo] = new Attribute(tmp);
	        				}
	        				break;
	        			}
					}
	        		
	        		
	        	}
//	        	else if(tmp.contains("M_M_M_C_C_C_labelPlayerFullName"))
//	        	{
//	        		player.setName(extractValue(tmp));
//	        	}
	        	else if(tmp.contains(
	        			"M_M_M_C_C_C_labelCharacterCreatedDateValue"))
	        	{
	        		player.setBirthDate(extractValue(tmp));
	        	}
	        	else if(tmp.contains("M_M_M_C_C_C_labelUserId"))
	        	{
	        		userId = "No user";
	        	}
	        	else if(tmp.contains("/User/UserGeneral/U53.aspx?id="))
	        	{
	        		tmp = tmp.substring(tmp.indexOf(
	        				"/User/UserGeneral/U53.aspx?id="));
	        		tmp = tmp.substring(tmp.indexOf(">") + 1, tmp.indexOf("<"));
	        		userId = tmp;
	        	}
//	        	else if(tmp.contains(
//	        			"M_M_M_C_C_C_labelAgeValue"))
//	        	{
//	        		player.setAge(Integer.parseInt(extractValue(tmp)));
//	        	}
//	        	else if(tmp.contains(
//						"M_M_M_C_C_C_labelCharacterTypeValue"))
//		    	{
//		    		player.setPosition(extractValue(tmp));
//		    	}
//	        	else if(tmp.contains(
//	        			"M_M_M_C_C_C_labelCurrentPlayerTransferValue"))
//	        	{
//	        		player.setValue(Integer.parseInt(extractValue(tmp)));
//	        	}
	        	
	        }
	        if(userId != null)
	        {
	        	player.setUserName(userId);
	        }
	        else
	        {
	        	player.setUserName(username);
	        }
	        player.setAttribs(attribs);
	        in.close();
	        if(player.isMyChar())
	        	get = new HttpGet(fidUrl + 
	        			"/Player/PlayerDevelopment/PlayerSkills.aspx");
	        else
	        	get = new HttpGet(fidUrl + 
	        			"/WorldAndCompetition/StatisticsAndAwards" +
	        			"/WorldPlayerSkills.aspx?id=" + 
	        			player.getId());
	        
	        page = client.execute(get, responseHandler);
	        in = new BufferedReader(
	        		new StringReader(page));
	        tmp = "";
	        String viewState = "";
	        String eventValidation = "";
	        while((tmp = in.readLine()) != null)
	        {
	        	if(tmp.contains("__VIEWSTATE"))
		    	{
//    				System.out.println(tmp);
		    		viewState = tmp.substring(
		    				tmp.lastIndexOf("value=\"") + 7,
		    				tmp.lastIndexOf("\""));
//    				System.out.println(viewState);
		    	}
		    	else if(tmp.contains("__EVENTVALIDATION"))
		    	{
//    				System.out.println(tmp);
		    		eventValidation = tmp.substring(
		    				tmp.lastIndexOf("value=\"") + 7,
		    				tmp.lastIndexOf("\""));
//    				System.out.println(eventValidation);
		    	}
		    	else
		    	{
		    		int value = 0;
		    		for (int i = 0; i < Player.specialAbilities.length; i++) {
		    			if(tmp.contains("SpecialAbilities_DXDataRow" + i))
		    			{
		    				while(!(tmp = in.readLine()).contains("SARating_A"));
		    				value = tmp.split("filledRatingStar").length - 1;
		    				if(value > 0)
		    					player.addSpecialAbility(
		    							Player.specialAbilities[i], value);
		    			}
					}
		    	}
	        }
	        in.close();
	        HttpPost httpost = null;
	        if(player.isMyChar())
	        	httpost = new HttpPost(fidUrl +
	        			"/Player/PlayerDevelopment/PlayerSkills.aspx");
	        else
	        	httpost = new HttpPost(fidUrl +
	        			"/WorldAndCompetition/StatisticsAndAwards" +
	        			"/WorldPlayerSkills.aspx?id=" + 
	        			player.getId());

	        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
	        
	        nvps.add(new BasicNameValuePair("__VIEWSTATE", viewState));
	        nvps.add(new BasicNameValuePair(
	        		"__EVENTVALIDATION", eventValidation));
	        nvps.add(new BasicNameValuePair(
	        		"__CALLBACKID", 
	        "M$M$M$C$C$C$playerSkillsControl$gridFootballerSpecialAbilities"));
	        nvps.add(new BasicNameValuePair(
	        		"__CALLBACKPARAM",
	        		"c0:GB|20;12|PAGERONCLICK3|PN1;"));
	        
	        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
	        page = client.execute(httpost, responseHandler);
	        
//	        PrintWriter p = new PrintWriter("test.html");
//	        p.write(page);
//	        p.close();
	        
	        String[] array = page.split("class=|style=");
	        int value = 0;
	        int abilityNo = -1;
	        int starCounter = 0;
	        for (int i = 0; i < array.length; i++) {
	        	for(int j = 10; j < Player.specialAbilities.length; j++)
	        	{
	        		if(array[i].contains("SpecialAbilities_DXDataRow" + j))
	        		{
	        			abilityNo = j;
	        			array[i] = "";
	        		}
	        	}
	        	
	        	if(array[i].contains("filledRatingStar"))
	        	{
	        		value++;
	        		starCounter++;
	        	}else if(array[i].contains("emptyRatingStar"))
	        	{
	        		starCounter++;
	        	}
	        	if(starCounter > 4)
	        	{
	        		if(value > 0)
	        			player.addSpecialAbility(
	        					Player.specialAbilities[abilityNo], value);
	        		value = 0;
	        		abilityNo = -1;
	        		starCounter = 0;
	        	}
			}
	        return true;
	        
		}
		catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	// Get required field from string.
	private String extractValue(String s)
	{
		return s.substring(s.lastIndexOf("\">") + 2, 
				s.indexOf("<", s.lastIndexOf("\">")));
	}
	
	// Fetch teams from the current competition.
	public LinkedList<Team> getTeams()
	{
		LinkedList<Team> teams = new LinkedList<Team>();
		try
		{
			get = new HttpGet(fidUrl + myLeaguePage);
			page = client.execute(get, responseHandler);
			in = new BufferedReader(new StringReader(page));
			tmp = "";
			String name = "";
			int id = 0;
			String tmpLeagueName = null;
			while((tmp = in.readLine()) != null)
			{
				// Get league name.
				if(tmp.contains("<label class=\"leagueInfoLabel\">"))
					tmpLeagueName = extractValue(tmp);
				// Get team id and name.
				else if(tmp.contains(teamPage)){
					
					tmp = tmp.substring(
							tmp.indexOf("href") + 6,
							tmp.indexOf("</a></td>")
							);
					id = Integer.parseInt(
							tmp.substring(
							tmp.indexOf("=") + 1, tmp.indexOf("\""))
							);
					name = tmp.substring(tmp.indexOf(">") + 1);
					// Add new team.
					teams.add(new Team(name, id));
				}
			}
			in.close();
			if(tmpLeagueName != null)
			{
				myLeague = tmpLeagueName;
			}
			else
			{
				return null;
			}
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
		return teams;
	}
	
	// Fetch players from a team.
	public LinkedList<Player> getPlayers(int id)
	{
		LinkedList<Player> players = new LinkedList<Player>();
		try{
			if(id == myId)
				get = new HttpGet(fidUrl + myTeamPlayersPage);
			else
				get = new HttpGet(fidUrl + teamPlayersPage + id);
			
			redirectUrl = "";
			
			page = client.execute(get, responseHandler);
			
			if(redirectUrl.contains("Team/TeamGeneral/U70.aspx"))
			{
				myId = id;
				return getPlayers(id);
			}
			
			in = new BufferedReader(new StringReader(page));
			int number = 0;
			int pId = 0;
			String name = "";
			String position = "";
			String aPosition = "";
			boolean online = false;
			Date lastOnline = null;
			int age = 0;
			double value = 0;
			tmp = "";
			while((tmp = in.readLine()) != null)
			{
				if(tmp.contains(
						"M_M_M_C_C_C_TeamPlayers_gridViewPlayersU72_DXDataRow"))
				{
					tmp = in.readLine();
					
					String[] s = tmp.split("[<>\"]");
					
					int i = 0;
					while(!s[i++].equals(""));
					number = Integer.parseInt(s[i]);
					
					try
					{
						int j = i;
						while(!s[j++].contains("name="));
						i = j;
					}
					catch (IndexOutOfBoundsException e)
					{
						online = false;
						lastOnline = new Date(0);
					}
					online = checkOnlineStatus(Integer.parseInt(s[i]));
					lastOnline = getLastOnline(Integer.parseInt(s[i]));

					while(!s[++i].contains("PlayerGeneral/General/U59.aspx?id="));
					pId = Integer.parseInt(s[i].substring(s[i].indexOf("=") + 1));
					
					i += 2;
					name = s[i];
					
					while(!s[++i].matches("[A-Z]+"));
					position = s[i];
					try
					{
						int j = i;
						while(!s[++j].matches("^[A-Z][A-Z].*"));
						aPosition = s[j];
						i = j;
					}
					catch(ArrayIndexOutOfBoundsException e)
					{
						aPosition = "";
						while(!s[i++].equals("left"));
					}
					
					
					while(!s[i++].equals("left"));
					while(!s[i++].equals(""));
					age = Integer.parseInt(s[i]);
					
					while(!s[i++].equals("left"));
					while(!s[i++].equals(""));
					s[i] = s[i].replace(',', '.');
					value = Double.valueOf(s[i]);
					
					players.add(new Player(number, pId, name, position,
							aPosition, age, value));
					players.getLast().setOnline(online);
					players.getLast().setLastOnline(lastOnline);
				
				}
			}
			in.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
		return players;
	}
	
	public String getMyLeague() {
		return myLeague;
	}
	public void setMyLeague(String myLeague) {
		this.myLeague = myLeague;
	}
	
	public boolean checkOnlineStatus(int id)
	{
		boolean result = false;
		get = new HttpGet(
				"http://chat.footballidentity.com/Shared/GetStatus.ashx?oid="
				+ id);
		try {
			String page = client.execute(get, responseHandler);
			if(page.contains("1"))
				result = true;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public Date getLastOnline(int id)
	{
		Date result = null;
		get = new HttpGet(
			"http://chat.footballidentity.com/Shared/GetLastActivity.ashx?oid="
				+ id);
		try {
			String page = client.execute(get, responseHandler);
			return TimeManagement.getTM().getDate(page);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public void updateOnlineStatuses(Team team)
	{
		try{
			if(team.getId() == myId)
				get = new HttpGet(fidUrl + myTeamPlayersPage);
			else
				get = new HttpGet(fidUrl + teamPlayersPage + team.getId());
			redirectUrl = "";
			page = client.execute(get, responseHandler);
			
			if(redirectUrl.contains("Team/TeamGeneral/U70.aspx"))
			{
				myId = team.getId();
				updateOnlineStatuses(team);
				return;
			}
			
			in = new BufferedReader(new StringReader(page));
			String name = "";
			boolean online = false;
			Date lastOnline = null;

			tmp = "";
			while((tmp = in.readLine()) != null)
			{
				if(tmp.contains(
					"M_M_M_C_C_C_TeamPlayers_gridViewPlayersU72_DXDataRow"))
				{
					tmp = in.readLine();
					
					String[] s = tmp.split("[<>\"]");
					
					int i = 0;
					try
					{
						while(!s[i++].contains("name="));
						i = Integer.parseInt(s[i]);
						online = checkOnlineStatus(i);
						lastOnline = getLastOnline(i);
					}
					catch (Exception e)
					{
						online = false;
						lastOnline = new Date(0);
						e.printStackTrace();
					}
					try
					{
						i = 0;
						while(!s[i++].contains(
								"/Player/PlayerGeneral/General/U59.aspx"));
						i++;
						name = s[i];
						Player p = team.getPlayer(name);
						if(p != null)
						{
							p.setOnline(online);
							p.setLastOnline(lastOnline);
						}
					}
					catch (ArrayIndexOutOfBoundsException e)
					{
						e.printStackTrace();
					}
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
					
	}
	
	public String getMatchStats(int id)
	{
		String homeOrAway = "home";
		String teamName = "";
		StringBuilder stats = new StringBuilder();
		for (int i = 0; i < 2; i++) 
		{
			get = new HttpGet(fidUrl + matchStatsPage + id + "&tp=" + homeOrAway);
			
			try {
				page = client.execute(get, responseHandler);
				in = new BufferedReader(new StringReader(page));
				tmp = "";
				String data = "";
				while((tmp = in.readLine()) != null)
				{
					if(tmp.contains("TeamCaption"))
					{
						int begin = tmp.indexOf("\">") + 2;
						int end = tmp.indexOf("<", begin);
						teamName = tmp.substring(begin, end);
					}
					if(tmp.contains("/Player/PlayerGeneral/General/U59.aspx?id=")
							&& tmp.contains("StatisticsDefence"))
					{
						stats.append(teamName + "_");
						int position = tmp.indexOf(
								playerPage);
						String playerId = tmp.substring(position);
						playerId = playerId.substring(
								playerId.indexOf("=") + 1,
								playerId.indexOf("\""));
						stats.append(fidUrl + playerPage + playerId + "_");
						int beginIndex = 0;
						int endIndex = 0;
						while((beginIndex = tmp.indexOf(">", position) + 1) != 0)
						{
							position = beginIndex + 1;
							if((endIndex = tmp.indexOf("<", beginIndex)) != -1)
							{
								data = tmp.substring(beginIndex, endIndex);
								if(!data.equals(""))
								{
									if (tmp.contains("StatisticsDefence"))
									{
										stats.append(data + "_");
									}
								}
							}
							else
							{
								if (tmp.contains("StatisticsDefence"))
									stats.append("\n");
								
								
							}
						}
					}
					
				}
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			homeOrAway = "away";
		}
		return stats.toString();
	}
	
	public String teamTraining()
	{
		StringBuilder output = new StringBuilder();
		
		try
		{
//			HttpPost post = new HttpPost("http://footballidentity.com" +
//					"/Team/TeamGeneral/TeamLockerRoomMatchSignup.aspx/SignUp");
//			StringEntity entity = new StringEntity(
//					"{\"match\":351543,\"character\":\"33364\"," +
//					"\"signupstatus\":\"1\"}",
//					"application/json",
//					HTTP.UTF_8);
			
			HttpPost post = new HttpPost("http://footballidentity.com/" +
					"Character/CharacterGeneral/TeamTrainingLinkSvc.asmx/" +
					"PerformTeamTraining");
			String ip = getExternalIp();
			output.append(ip + "<br>");
			System.out.println(ip);
			StringEntity entity = new StringEntity("{\"host\":\"" + ip + "\"}",
					"application/json",
					HTTP.UTF_8);
			post.setEntity(entity);
			HttpResponse response = client.execute(post);
			System.out.println(response.getStatusLine());
			output.append(response.getStatusLine() + "<br>");
			System.out.println(ArrayUtils.toString(response.getAllHeaders()));
			output.append(ArrayUtils.toString(response.getAllHeaders()) + "<br>");
			in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			String line = null;
			while((line = in.readLine()) != null)
			{
				System.out.println(line);
				output.append(line + "<br>");
			}
			EntityUtils.consume(entity);
			in.close();
			
		}
		catch (UnsupportedEncodingException e)
		{
			output.append(e);
			e.printStackTrace();
		} 
		catch (ClientProtocolException e) {
			e.printStackTrace();
			output.append(e);
		}
		catch (IOException e) {
			e.printStackTrace();
			output.append(e);
		}
		
		
		

		return output.toString();
	}
	
	public String attributeTraining(int attribId)
	{
		String returnValue = null;
		try {
			
			// Get training page.
			get = new HttpGet(fidUrl +
			"/Player/PlayerGeneral/UA1.aspx");

		    page = client.execute(get, responseHandler);
		    
		    // Get Attribute training page.
		    in = new BufferedReader(new StringReader(page));
		    tmp = "";
		    while((tmp = in.readLine()) != null)
		    {
		    	if(tmp.contains("__VIEWSTATE"))
		    	{
//    				System.out.println(tmp);
		    		viewState = tmp.substring(
		    				tmp.lastIndexOf("value=\"") + 7,
		    				tmp.lastIndexOf("\""));
//    				System.out.println(viewState);
		    	}
		    	else if(tmp.contains("__EVENTVALIDATION"))
		    	{
//    				System.out.println(tmp);
		    		eventValidation = tmp.substring(
		    				tmp.lastIndexOf("value=\"") + 7,
		    				tmp.lastIndexOf("\""));
//    				System.out.println(eventValidation);
		    	}
		    }
			
			HttpPost httpost = new HttpPost(fidUrl +
					"/Player/PlayerGeneral/UA1.aspx");
	
	        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
	        
	        nvps.add(new BasicNameValuePair("__VIEWSTATE", viewState));
	        
	        nvps.add(new BasicNameValuePair(
	        		"__EVENTVALIDATION", eventValidation));
	        nvps.add(new BasicNameValuePair("__EVENTTARGET",
	        							"M$M$M$C$C$C$headerPanel$trainType"));
	        nvps.add(new BasicNameValuePair("M$M$M$C$C$C$headerPanel$trainType", 
	        								"Attribute training"));
	
	    
			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
			HttpResponse response = client.execute(httpost);
			if(!response.getStatusLine().toString().equals(
					"HTTP/1.1 200 OK"))
			{
				EntityUtils.consume(response.getEntity());
				throw new Exception("Something got wrong (" +
						response.getStatusLine().toString() + ").");
			}
			// Get training parameters.
			
//			PrintWriter out = new PrintWriter(new File("response.html"), "UTF-8");
			String tmp = null;
			BufferedReader in = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent(), "UTF-8"));
			
			nvps = new ArrayList<NameValuePair>();
//			HashMap<String, Integer> attribIds = new HashMap<String, Integer>();
//			String[] attribNames = new String[Player.N_ATTRIBS];
//			int id = -1;
//			int attribCounter = 0;
			
			nvps.add(new BasicNameValuePair(
					"__CALLBACKID", 
					"M$M$M$C$C$C$headerPanel$gridViewAttributeTraining"));
			nvps.add(new BasicNameValuePair(
					"__CALLBACKPARAM", "c0:GB|25;14|CUSTOMCALLBACK6|" 
					+ attribId 
					+ ";"));
			
			while((tmp = in.readLine()) != null )
			{
				
//				out.append(tmp + "\n");
				if(tmp.contains("__VIEWSTATE"))
				{
					nvps.add(new BasicNameValuePair("__VIEWSTATE", 
													tmp.split("\"")[7]));
				}
				else if(tmp.contains("CallbackState"))
				{
					nvps.add(new BasicNameValuePair(
							"M$M$M$C$C$C$headerPanel$" +
							"gridViewAttributeTraining$CallbackState", 
							tmp.split("\"")[7]));
				}
				else if(tmp.contains("__EVENTVALIDATION"))
				{
					nvps.add(new BasicNameValuePair("__EVENTVALIDATION", 
							tmp.split("\"")[7]));
				}
//				else if(tmp.contains("attributeId ="))
//				{
//					tmp = tmp.trim();
//					id = Integer.parseInt(tmp.split("[ ;]")[2]);
//					tmp = in.readLine();
//					attribNames[attribCounter] = tmp.split("\"")[1];
//					attribIds.put(attribNames[attribCounter++], id);
//				}
			}
			nvps.add(new BasicNameValuePair(
					"M$M$M$C$C$C$headerPanel$trainType", 
					"Attribute training"));
			
//			for (int i = 0; i < nvps.size(); i++) {
//				System.out.println(nvps.get(i));
//			}
//			for (String name : attribNames) {
//				System.out.println(name + "=" + attribIds.get(name));
//			}
			
//			out.close();
			EntityUtils.consume(response.getEntity());
			
			// Perform training.
			httpost = new HttpPost(fidUrl +
			"/Player/PlayerGeneral/UA1.aspx");
			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
			response = client.execute(httpost);
			if(!response.getStatusLine().toString().equals(
			"HTTP/1.1 200 OK"))
				{
					EntityUtils.consume(response.getEntity());
					throw new Exception("Something got wrong.");
				}
			EntityUtils.consume(response.getEntity());
			
			// Get the result of training.
			nvps.set(0, new BasicNameValuePair("__CALLBACKID", 
					"M$M$M$C$C$C$headerPanel"));
			nvps.set(1, new BasicNameValuePair("__CALLBACKPARAM", "c0:"));
			
			httpost = new HttpPost(fidUrl +
			"/Player/PlayerGeneral/UA1.aspx");
			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
			response = client.execute(httpost);
			in = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent(), "UTF-8"));
			tmp = in.readLine();
			String[] array = tmp.split("(\">)|(</)");
			if(array.length >= 12)
				returnValue = array[11];
			if(returnValue.contains("label"))
				if(array.length >= 17)
					returnValue = array[16] + ".";
			
			EntityUtils.consume(response.getEntity());
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnValue;
	}
	
	// Shut down the connection, should be called when all online work is done.
	public void shutDownConnection(){
		client.getConnectionManager().shutdown();
	}
	
	public String getExternalIp()
	{
		String ip = "0.0.0.0";
		try {
			get = new HttpGet("http://api-ams01.exip.org/?call=ip");
			page = client.execute(get, responseHandler);
			in = new BufferedReader(new StringReader(page));
			ip = in.readLine();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return ip;
	}

	public static void main(String[] args) {
		FIDataFetcher f = new FIDataFetcher("xxx", "xxx");
		System.out.println(f.login());
		System.out.println(f.attributeTraining(87));
	}
	public static void main1(String[] args) {
		FIDataFetcher f = null;
		if(args.length == 2)
			f = new FIDataFetcher(args[0], args[1]);
		else
		{
			// Usage 
			System.out.println("Incorect number of arguments provided.");
			System.out.println(
					"Usage: java -jar fidDataFetcher.jar username password");
			System.exit(4);
		}
		
		PrintWriter out = null;
		File dir = null;
		Scanner in = null;
		int exit = -1;
		int teamNumber = -1;
		if(f.login() == 0)
			System.out.println(
					"Successfully loged on to FID with username: "
					+ args[0]);
		else
		{
			System.out.println("Invalid username or password.");
			System.exit(1);
		}
		LinkedList<Team> teams = f.getTeams();
		while(exit != 1)
		{
			exit = -1;
			if(teams != null)
			{
				System.out.println("Teams from " + f.getMyLeague() + " :");
				for (int i = 0; i < teams.size(); i++) {
					System.out.println((i + 1) + ". " + teams.get(i));
				}
				System.out.println();
				teamNumber = -1;
				while(teamNumber == -1)
				{
					System.out.println(
							"Enter a team number whoose " +
							"players you want to download,\n" +
							"or 0 if u want to download all teams.");
					System.out.println();
					try
					{
						in = new Scanner(System.in);
						teamNumber = in.nextInt();
						if(teamNumber < 0 || teamNumber > teams.size())
						{
							in.close();
							throw new Exception();
						}
					}
					catch(Exception e)
					{
						System.out.println("Wrong choice, please try again.");
						teamNumber = -1;
					}
				}
				System.out.println("Your choice was: " + teamNumber);
				dir = new File(f.myLeague);
				if(dir.mkdir() == true)
					System.out.println("Created directory:\n" 
							+ dir.getAbsolutePath());
			}
			else
			{
				System.out.println("Error fetching teams.");
				System.exit(2);
			}
			for (int i = 0; i < teams.size(); i++) {
	//			out = new PrintWriter()
				if((teamNumber-1) == i || teamNumber == 0)
					try {
						Team curTeam = teams.get(i);
						out = new PrintWriter(new File(dir.getName()
								+ "/" + curTeam.getName() + ".txt"), "UTF-8");
						out.println(curTeam.getName()); 
						out.println(curTeam.getId());
						out.println();
						LinkedList<Player> players = null;
						System.out.println(
								"Fetching players from: " 
								+ curTeam.getName());
						players = f.getPlayers(curTeam.getId());
						if(players != null)
						{
							curTeam.addPlayers(players);
							for (int j = 0; j < players.size(); j++) {
								Player curPlayer = players.get(j);
								if(f.getPlayer(curPlayer) == true)
								{
									out.println(curPlayer.toStringFull());
									out.println();
									out.println("--------------------------" +
											"------------------------------");
									out.println();
								}
								else
								{
									System.out.println("Error fetching player "
											+ curPlayer.getName());
									out.println("Error fetching player "
											+ curPlayer.getName());
									out.println();
									out.println("--------------------------" +
									"------------------------------");
									out.println();
								}
							}
						}
						else
						{
							System.out.println("Error fetching players from: "
									+ curTeam.getName());
						}
						out.close();
					} catch (FileNotFoundException e) {
					} catch (UnsupportedEncodingException e) {
						
					}
				
			
			}
	//		for (int i = 0; i < players.size(); i++) {
	//			f.fetchPlayer(players.get(i));
	//			System.out.println(players.get(i) + "\n");
	//		}
			System.out.println();
			System.out.println("Finished operation.");
			
			while(exit != 0 && exit != 1)
			{		
				in = new Scanner(System.in);
				System.out.println("Type 1 to exit, " +
						"or 0 to return to team choice.");
				try
				{
					exit = in.nextInt();
					
				}
				catch(Exception e)
				{
					System.out.println("Wrong choice. Please try again.");
					exit = -1;
				}
			}
		}
		in.close();
		f.shutDownConnection();
	}

}
